package Java.view.game.zhajinhua;

import Java.view.game.doudizhurule.Common;
import domain.Poker;

import java.awt.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class pk {
    private static final int SAME_POINT = 60 * 100000;
    private static final int STRAIGHT = 20 * 100000;
    private static final int SAME_COLOR = 30 * 100000;
    private static final int SAME_COLOR_STRAIGHT = STRAIGHT + SAME_COLOR;
    private static final int PAIR = 10 * 100000;

    /**
     * 比牌
     *
     * @param handCards
     * @return 根据牌型以及牌面返回一个int
     */
    public int judge(List<Poker> handCards) {//返回一个七位数代表牌面大小
        Collections.sort(handCards);//实现了Comparable 降序
        int value = 0;

        //后六位 点数
        if (isPair(handCards)) {
            if (handCards.get(0).getScore().num == handCards.get(1).getScore().num)
                value += handCards.get(0).getScore().num * 10000
                        + handCards.get(0).getScore().num * 100
                        + handCards.get(2).getScore().num;
            else
                value += handCards.get(1).getScore().num * 10000
                        + handCards.get(1).getScore().num * 100
                        + handCards.get(0).getScore().num;

        } else {//单牌
            value += handCards.get(0).getScore().num * 10000
                    + handCards.get(1).getScore().num * 100
                    + handCards.get(2).getScore().num;
        }

        //最高位 牌型
        if (isSameScore(handCards))//豹子 6
            value += SAME_POINT;
        else if (isSameColorStraight(handCards))//同花顺 5
            value += SAME_COLOR_STRAIGHT;
        else if (isSameColor(handCards))//金花 3
            value += SAME_COLOR;
        else if (isStraight(handCards))//顺子 2
            value += STRAIGHT;
        else if (isPair(handCards)) {//对子 1
            value += PAIR;
        } else {
            //单牌
        }
        return value;
    }

    private boolean isSameColor(List<Poker> handCards) {//同花
        return handCards.get(0).getSuit() == handCards.get(1).getSuit() &&
                handCards.get(1).getSuit() == handCards.get(2).getSuit();
    }

    private boolean isPair(List<Poker> handCards) {//对子
        return handCards.get(0).getScore().getNum() == handCards.get(1).getScore().getNum()
                || handCards.get(1).getScore().getNum() == handCards.get(2).getScore().getNum()
                || handCards.get(0).getScore().getNum() == handCards.get(2).getScore().getNum();
    }

    private boolean isStraight(List<Poker> handCards) {//顺子
        Collections.sort(handCards);
        return Math.abs(handCards.get(0).getScore().getNum() - handCards.get(1).getScore().getNum()) == 1
                && Math.abs(handCards.get(1).getScore().getNum() - handCards.get(2).getScore().getNum()) == 1;

    }

    private boolean isSameColorStraight(List<Poker> handCards) {//顺金
        return isSameColor(handCards) && isStraight(handCards);
    }

    private boolean isSameScore(List<Poker> handCards) {//豹子
        return handCards.get(0).getScore() == handCards.get(1).getScore()
                && handCards.get(2).getScore() == handCards.get(1).getScore();

    }



}
